<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item>
          <el-input v-model="filters.keyword" search="关键字"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="search">查询</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表展示-->
    <el-table @row-dblclick="getClick" :data="brandTypes" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="name" label="数据字典名" width="120" >
      </el-table-column>
      <el-table-column prop="sn" label="数据字典编号" width="100" >
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template slot="header" scope="scope">
          <span>操作&emsp;&emsp;</span>
          <el-button size="middle" type="primary" @click="handleAdd">新增</el-button>
        </template>
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange"
                     :current-page="currentPage"
                     :page-size="pageSize"
                     :total="total" style="float:right;">
      </el-pagination>
    </el-col>

    <!--编辑或新增界面
        v-model：在1.X的时候是控制此标签是否展示，但是2.X之后是双向绑定
    -->
    <el-dialog :title="title" :visible.sync="editFormVisible" :close-on-click-modal="false">
      <el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
        <el-form-item label="字典名" prop="name">
          <el-input v-model="editForm.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="字典编号" prop="sn">
          <el-input v-model="editForm.sn" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="editFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
      </div>
    </el-dialog>

    <!--  抽屉  -->
    <el-drawer
        :title="tableTitle"
        :visible.sync="table"
        direction="rtl"
        size="50%">

    <!--列表展示-->
    <el-table :data="gridData" highlight-current-row v-loading="detailListLoading" @selection-change="detailSelsChange" style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" label="编号" width="60">
      </el-table-column>
      <el-table-column prop="name" label="明细名" min-width="500" align="center" >
      </el-table-column>
      <el-table-column  label="操作" width="200">
        <template slot="header" scope="scope">
          <span>操作&emsp;&emsp;</span>
          <el-button size="middle" type="primary" @click="detailAdd">新增</el-button>
        </template>
        <template scope="scope">
          <el-button size="small" @click="detailEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="detailDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="detailBatchRemove" :disabled="this.detailSels.length===0">批量删除</el-button>
      <el-pagination layout="total, sizes, prev, pager, next" :page-sizes="[5, 10, 15, 20]" @size-change="detailSizeChange" @current-change="detailCurrentChange" :current-page="detailCurrentPage" :page-size="detailPageSize" :total="detailTotal" style="float:right;">
      </el-pagination>
    </el-col>
    </el-drawer>



    <!-- 抽屉结束 -->

  </section>
</template>

<script>
import util from '../../common/js/util'
//import NProgress from 'nprogress'
import { getBrandTypeListPage, removeBrandType, batchRemoveBrandType, editBrandType, addBrandType } from '../../api/api';

export default {
  data() {
    return {
      tableTitle:"", // 抽屉
      table: false, // 抽屉弹出
      gridData:[], // 获取后端返回数据
      detailTitle:"", // 添加和编辑明细。赋默认值，有下方调用赋值

      // 上级级联的集合数据
      options: [],
      filters: {
        keyword: ''
      },
      rowId:0,
      title:"", // 编辑和新增界面 显示的编辑还是新增内容。下边应用，点击新增复制新增，点击编辑赋值编辑
      brandTypes: [],
      total: 0, // 当前页
      pageSize: 5,
      currentPage:1,
      listLoading: false,
      sels: [],//列表选中列
      detailTotal: 0,

      // 抽屉忙等
      detailListLoading: false,
      // 编辑界面是否显示
      detailEditFormVisible: false,
      // 抽屉分页
      detailCurrentPage: 1,
      detailPageSize: 5,
      // 抽屉中列表选中列
      detailSels: [],

      editFormVisible: false,//编辑界面是否显示
      editLoading: false,
      editFormRules: {
        name: [ // 校验姓名和编号
          { required: true, message: '请输入数据字典名称', trigger: 'blur' }
        ],
        sn: [ // 校验姓名和编号
          { required: true, message: '请输入数据字典编号', trigger: 'blur' }
        ],
      },

      //编辑界面数据
      editForm: {
        id:null,
        sn:'',
        name:"",

      // 抽屉中添加、编辑明细界面数据
      detailEditForm:{
        id: null,
        name: "",
        types_id: ""
      }

      },
      cities: []
    }

  },
  methods: {
    // 抽屉列表绑定双击事件
    getClick(row){
      // 弹出抽屉
      this.table = true;
      // 给表格标题赋值
      this.tableTitle = row.name;
      // 获取当前行的id并赋值给rowId
      this.rowId = row.id;
      // 给抽屉中的页面默认为1
      this.detailCurrentPage = 1;
      // 发送axios请求，获取对应的字典明细
      this.getDetails();

    },

    // 获取字典明细值
    getDetails(){
      let para = {
        detailCurrentPage: this.detailCurrentPage,
        detailPageSize: this.detailPageSize,
        typesId: this.rowId,
      };
      // 开启列表忙等
      this.detailListLoading = true;
      // 发送axios请求
      this.$http.post("/detail", para)
          .then(result=>{
            // 关闭列表忙等
            this.detailListLoading = false;
            // 获取后端返回的数据
            result = result.data;
            // 设置分页总条数
            this.detailTotal = result.total;
            // 获取后端返回的数据
            this.gridData = result.rows;
          });
    },


    // 高级查询函数
    search(){
      // 如果是高级查询，为了避免用户的数据不够展示，我们需要初始化页码为1，不管有没有其他页数据，我们默认展示第一页
      this.currentPage = 1;
      // 页面初始化完成之后，直接调用查询列表数据函数即可
      this.getBrandTypes();
    },

    // 分页每页展示条数
    handleSizeChange(val){
      this.pageSize = val;
      this.currentPage = 1;
      this.getBrandTypes();
    },

    // 当前分页
    handleCurrentChange(val) {
      // 当用户点击页面的时候，我们需要同步将模型层中的当前页面字段的值改为用户选中的值
      this.currentPage = val;
      this.getBrandTypes();
    },

    // 抽屉分页每页展示条数
    detailSizeChange(val){
      this.detailPageSize = val;
      this.detailCurrentPage = 1;
      this.getDetails();
    },


    // 抽屉中分页当前页
    detailCurrentChange(val) {
      this.detailCurrentPage = val;
      this.getDetails();
    },

    //获取数据字典类型列表
    getBrandTypes() {
      // 封装请求参数
      let para = {
        currentPage: this.currentPage,
        pageSize: this.pageSize,
        keyword: this.filters.keyword

      };
      // 开启列表忙等
      this.listLoading = true;
      // 发起Axios请求。调用的后方，超级查询加分页接口
      this.$http.post("/brandtype", para)
          .then(result=>{
            // 0.关闭忙等
            this.listLoading = false;
            // 1.给列表模型层对象赋值
            this.brandTypes = result.data.rows;
            // 2.给total赋值
            this.total = result.data.total;
          })
          .catch(result=>{
            this.$message({
              message: '系统繁忙，请重试！',
              type: 'error'
            });
          })


    },
    //删除
    handleDel: function (index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        // 发送axios请求
        this.$http.delete("/brandtype/"+row.id)
          .then(result=>{
            // 关闭忙等
            this.listLoading = false;
            if (result.data.success){
              this.$message({
                message: result.data.message,
                type: 'success'
              });
            } else {
              this.$message({
                message: result.data.message,
                type: 'error'
              });
            }
            // 回到第一页
            this.currentPage = 1;
            this.getBrandTypes();
          })
            .catch(result=>{
              this.listLoading = false;
              this.$message({
                message: '系统繁忙，请重试！',
                type: 'error'
              });
            })
      }).catch(() => {
      });
    },

    // 抽屉删除
    detailDel: function (index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        // 开启列表忙等
        this.detailListLoading = true;
        // 发起axios请求
        this.$http.delete("/brandtype/" + row.id)
            .then(result=>{
              // 关闭列表忙等
              this.detailListLoading = false;
              if(result.data.success) {
                // 展示删除成功信息
                this.$message({
                  message: result.data.message,
                  type: 'success'
                });
              } else { // 我们不确定是否真的删除成功，一律按删除成功处理
                this.$message({
                  message: result.data.message,
                  type: 'error'
                });
              }
              // 初始化detailCurrentPage=1
              this.detailCurrentPage = 1;
              // 调用抽屉中查询列表函数
              this.getBrandTypes();

            })
            .catch(result=>{
              // 1.关闭页面加载忙等
              this.detailListLoading = false;
              this.$message({
                message: '系统繁忙，请重试',
                type: 'error'
              });
            })
      }).catch(() => {

      });
    },




    //显示编辑界面
    handleEdit: function (index, row) {
      // 框架属性，是否显示框架
      this.title = "编辑数据字典";
      this.editFormVisible = true;

      //  使用clearValidate移除表单验证结果
      this.$nextTick(() => {
        this.$refs["editForm"].clearValidate();
      });
      this.editFormVisible = true;
      this.editForm = Object.assign({}, row);

    },

    //显示新增界面//
    handleAdd: function () {
      this.title = "新增数据字典";
      //  使用clearValidate移除表单验证结果
      this.$nextTick(() =>{
        this.$refs["editForm"].clearValidate();
      });

      this.editFormVisible = true;
      this.editForm = {
        id:null,
        name: "",
        sn: ""

      };
    },

    //显示数据字典新增界面
    handleAdd2: function () {
      this.title = "新增";
      this.editFormVisible = true;
      this.editForm = {
        id:null,
        name:"",
        types_id:'1',
        manager:{}

      };
    },

    // 显示抽屉中字典明细 新增界面
    detailAdd:function (val){
      this.detailTitle = "添加明细";
      this.$nextTick(()=>{
        this.$refs["detailEditForm"].clearValidate();
      });
      this.detailEditFormVisible = true;
      this.detailEditForm = {
        id: null,
        name: "",
        types_id: ""
      };
    },

    // 显示字典编辑界面
    detailEdit:function (index, row) {
      this.detailTitle = "编辑明细";
      this.$nextTick(()=>{
        this.$refs["detailEditForm"].clearValidate();
      });
      this.detailEditFormVisible = true;
      // 克隆选中的数据
      this.detailEditForm = Object.assign({}, row);
    },


    //编辑和新增
    editSubmit: function () {
      // 开启表单校验
      this.$refs.editForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.editLoading = true; // 1.开启忙等
            // 2.从模型层中的editform对象克隆一个新的对象
            let para = Object.assign({}, this.editForm);
            
            // 5.发送axios请求
            this.$http.put("/brandtype",para)
              .then(result=>{
                // 6.关闭按钮忙等
                this.editLoading = false;
                // 7.关闭模态框
                this.editFormVisible = false
                // 8.新增修改成功
                if (result.data.success) {
                  this.$message({
                    message: result.data.message,
                    type:'success'
                  });
                } else {
                  this.$message({
                    message: result.data.message,
                    type:'error'
                  });
                }
                // 9.改变当前页码
                this.currentPage = 1;
                // 10.调用查询列表
                this.getBrandTypes();
              })
              .catch(result=>{
                this.editLoading = false;
                this.$message({
                  message: '系统繁忙，请重试！',
                  type: 'error'
                });
              })
          });
        }
      });
    },


    selsChange: function (sels) {
      // 将用户选中的所有数据赋值给sels
      this.sels = sels;
    },
    //批量删除
    batchRemove: function () {
      var ids = this.sels.map(item => item.id);
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        // 发送axios请求
        this.$http.patch("/brandtype",ids)
            .then(result=>{
              // 关闭忙等
              this.listLoading = false;
              if (result.data.success){
                this.$message({
                  message: result.data.message,
                  type: 'success'
                });
              } else {
                this.$message({
                  message: result.data.message,
                  type: 'error'
                });
              }
              // 回到第一页
              this.currentPage = 1;
              this.getBrandTypes();
            })
            .catch(result=>{
              this.listLoading = false;
              this.$message({
                message: '系统繁忙，请重试！',
                type: 'error'
              });
            })

      }).catch(() => {

      });
    },

    // 抽屉中的批量删除
    detailSelsChange:function (detailSels){
      this.detailSels = detailSels;
      },




    //批量删除
    batchRemove2: function () {
      var ids = this.sels.map(item => item.id);
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        // 发送axios请求
        this.$http.patch("/brandType",ids)
            .then(result=>{
              // 关闭忙等
              this.listLoading = false;
              if (result.data.success){
                this.$message({
                  message: result.data.message,
                  type: 'success'
                });
              } else {
                this.$message({
                  message: result.data.message,
                  type: 'error'
                });
              }
              // 回到第一页
              this.currentPage = 1;
              this.getBrandTypes();
            })
            .catch(result=>{
              this.listLoading = false;
              this.$message({
                message: '系统繁忙，请重试！',
                type: 'error'
              });
            })

      }).catch(() => {

      });
    },

    // 抽屉中数据批量删除
    detailBatchRemove: function () {
      var ids = this.detailSels.map(item => item.id);
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        // 开启列表忙等
        this.detailListLoading = true;
        // 发送Axios请求
        this.$http.patch("/systemDictionaryDetail", ids)
            .then(result=>{
              // 关闭列表忙等
              this.detailListLoading = false;
              if(result.data.success) {
                // 展示删除成功信息
                this.$message({
                  message: result.data.message,
                  type: 'success'
                });
              } else { // 我们不确定是否真的删除成功，一律按删除成功处理
                this.$message({
                  message: result.data.message,
                  type: 'error'
                });
              }
              // 初始化currentPage=1
              this.detailCurrentPage = 1;
              // 调用查询列表函数
              this.getDetails();

            })
            .catch(result=>{
              // 1.关闭页面加载忙等
              this.detailListLoading = false;
              this.$message({
                message: '系统繁忙，请重试',
                type: 'error'
              });
            })
      }).catch(() => {

      });
    },



  },
  mounted() {
    //
    this.getBrandTypes();

  }
}

</script>

<style scoped>

</style>